(function($){
	var filter_team = function(id){
		$.getJSON("/filter_team/", {'id': id}, function(data){
			var html = "<option value=''>---------</option>";
			$.each(data.res, function(k, v){
				if(v.id == $("#id_team").val()){
					html += "<option selected='selected' value='" + v.id + "'>" + v.name + "</option>";
				}else{
					html += "<option value='" + v.id + "'>" + v.name + "</option>";
				}
			});
			$("#id_team").html(html);
		});
	}

	var filter_pjms = function(id){
		$.getJSON("/filter_pjms/", {'id': id}, function(data){
			var html = "<option value=''>---------</option>";
			$.each(data.res, function(k, v){
				if(v.id == $("#id_pjm").val()){
					html += "<option selected='selected' value='" + v.id + "'>" + v.name + "</option>";
				}else{
					html += "<option value='" + v.id + "'>" + v.name + "</option>";
				}
			});
			$("#id_pjm").html(html);
		});
	}

	var filter_studio = function(id){
		$.getJSON("/filter_pjm_studio/", {'id': id}, function(data){
			var html = "<option value=''>---------</option>";
			$.each(data.res, function(k, v){
				if(v.id == $("#id_studio").val()){
					html += "<option selected='selected' value='" + v.id + "'>" + v.name + "</option>";
				}else{
					html += "<option value='" + v.id + "'>" + v.name + "</option>";
				}
			});
			$("#id_studio").html(html);
		});
	}

	$(document).ready(function(){
		$("th.get_prd_status-column ").css('width', '38px');
		$("th.get_art_status-column ").css('width', '38px');
		$("th.get_content_status-column ").css('width', '38px');
		$("th.get_ui_ue_status-column ").css('width', '38px');
		$("th.get_mkt_status-column ").css('width', '38px');
		$("th.get_sound_status-column ").css('width', '38px');
		$("th.get_code_status-column ").css('width', '38px');
		$("th.get_qa_status-column ").css('width', '38px');
		
		$("#result_list tbody tr th").css('vertical-align', 'middle');
		$("#result_list tbody tr td").css('vertical-align', 'middle');
		
		$("div.WTS").parent().css('background-color', '#f03433');
		$("div.WIP").parent().css('background-color', '#fea700');
		$("div.DONE").parent().css('background-color', '#38d138');
		$("div.NO_NEED").parent().css('background-color', 'rgb(226, 226, 226)');
		
		filter_team($("#id_division").val());
		filter_pjms($("#id_team").val());
		filter_studio($("#id_team").val());
		
		$("#id_division").change(function(e){
			$("#id_tasks_to").html("");
			$("#id_tasks_from").html("");
			filter_team(e.target.value);
			filter_pjms();
			filter_studio();
		});
		$("#id_team").change(function(e){
			$("#id_tasks_to").html("");
			$("#id_tasks_from").html("");
			filter_pjms(e.target.value);
			filter_studio(e.target.value);
		});
		
		$("#id_pjm").change(function(e){
			$("#id_tasks_to").html("");
			SelectBox.init('id_tasks_to');
			$.getJSON("/filter_tasks/", {'id': e.target.value}, function(data){
				var html = "";
				$.each(data.res, function(k, v){
					html += "<option value='" + v.id + "'>" + v.name + "</option>"; 
				});
				$("#id_tasks_from").html(html);
				SelectBox.init('id_tasks_from');
				if(html != ""){
					$("#id_tasks_add_all_link").addClass('active');
				}else{
					$("#id_tasks_add_all_link").removeClass('active');
				}
			});
		});
		
	});
})(django.jQuery);